<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of home
 *
 * @author hoabui
 */
class home extends CI_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        set_validation_error_delimiter();
    }

    function index() {
        $this->load->view('tp-admin/login');
    }

    function login() {
        
        if ($this->input->post('submit') && $this->check_validation()) {
            $info = $this->post_input();
            $user_info=$this->user_model->login($info['user_name'], $info['password']);
            if ($user_info==null) {

                $this->session->set_userdata(array('error_msg' => 'Sai tên đăng nhập hoặc mật khẩu'));
                redirect(current_url());
            } else {
               
                $this->session->set_userdata(array('user_name' => $info['user_name'],'user_role'=>$user_info->role));
                redirect(HOME_PAGE.ADMIN_PAGE.'admin_product');
            }
        }
        $this->load->view('tp-admin/login');
    }

    function logout(){
        $user_name=$this->session->userdata('user_name');
        if(isset($user_name)){
            $this->session->unset_userdata(array('user_name'=>'','user_role'=>''));
            redirect(HOME_PAGE.ADMIN_PAGE.'home');
        }
                
                
    }
    function post_input() {
        $info = array(
            'user_name' => $this->input->post('user_name'),
            'password' => $this->input->post('password')
        );
        return $info;
    }

    function check_validation() {
        $check_validation = array(
            array(
                'field' => 'user_name',
                'label' => 'Tên đăng nhập',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'password',
                'label' => 'Mật khẩu',
                'rules' => 'trim|required|md5'
            )
        );
        $this->form_validation->set_rules($check_validation);
        return $this->form_validation->run();
    }

}

?>
